package db


import (
	db "hulk-api/databases"
	"log"
)

type SlowLog struct {
	Query_time_max string		`json:"query_time_max"`
	Query_time_min string		`json:"query_time_min"`
	Query_time_avg string		`json:"query_time_avg"`
	Sample string				`json:"sample"`
	Fingerprint string			`json:"fingerprint"`
	Ts_max string				`json:"ts_max"`
}


func (s SlowLog) GetSlowLog() ( slog []SlowLog , err error ) {
	//var slog SlowLog
	rows,err := db.SqlDB.Query("select a.ts_max,a.Query_time_max,a.Query_time_min,a.Query_time_pct_95,a.Sample,b.fingerprint from (select ts_max,checksum,Query_time_sum,Query_time_min,Query_time_max,Query_time_pct_95,sample from query_history where Query_time_sum in (select max(Query_time_sum) from query_history group by checksum)) as a , query_review as b where a.checksum=b.checksum" )
	if err != nil {
		log.Println(err)
		return
	}
	defer rows.Close()
	for rows.Next() {
		var s SlowLog
		rows.Scan(&s.Ts_max,&s.Query_time_max,&s.Query_time_min,&s.Query_time_avg,&s.Sample,&s.Fingerprint)
		slog = append(slog, s)
	}
	if err = rows.Err(); err != nil {
		log.Println(err)
		return
	}

	return
}
